package com.group3.merchant.utils;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.group3.entity.AMerchant;

public class JsonUtil {
	private static JsonUtil instance = null;
	static {
		instance = new JsonUtil();// 用空间换时间
	}

	public static JsonUtil getInstance() {
		return instance;
	}

	private static ObjectMapper mapper = new ObjectMapper();

	private JsonUtil() {

	}

	public String ObjectToJson(Object str) {
		String jsonStr = null;
		try {
			jsonStr = mapper.writeValueAsString(str);
		} catch (JsonProcessingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jsonStr;
	}

	public <T> T getObjectFromJson(String json, Class<T> clazz) {
		try {
			return mapper.readValue(json, clazz);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}
